/*
  *************************************************************
  * @file    sl2x23.c
  * @author     embedfire
  * @version     V1.0
  * @date        2023
  * @brief   sl2x23NFC接口
  *************************************************************
  * @attention
  *
  * 论坛      ：http://www.firebbs.cn
  * 官网      ：https://embedfire.com/
  * 淘宝      ：https://yehuosm.tmall.com/
  *
  ******************************************************************************
*/
#include "emv_if.h"
#include "sl2x23.h"
#include "Rfid_interface.h"
#include "lpcd.h"
// #include "dwt/bsp_dwt_delay.h" 

extern uint8_t  card_A;
extern uint8_t  card_B;
extern uint8_t  card_F;
extern uint8_t  card_V;
extern uint8_t  sak;
extern uint16_t atqa;
int8_t nfc_demo(void) 
{
    volatile uint8_t card_det = 0, g_ret, demo_step = 0;
    while(1) {
        switch(demo_step) {
            case 0://询卡
                card_A = 0;
                card_B = 0;
                card_F = 0;
                card_V = 0;
                g_ask_block_flag = 0;
                #ifdef SPD_MODE
                    pcd_sleep_exit(); //Soft power on
                #endif
                pcd_set_rate(1);
                g_ret = pcd_polling(g_tag_info.serial_num);
                if(g_ret == 0) 
                    demo_step = 1;
                break;
            case 1:
                if(card_A) {
                    printf("sak == %d\r\n\r\n",sak);
                    
                    if((sak == 0x08) || (sak == 0x18)) 
                    { //MIFARE
                        NFC_A_Mifare_READ_ALL_Test(g_tag_info.serial_num);
                        demo_step = 0;
                        card_det  = 1;
                        break;
                    }
                    printf("atqa == %d\r\n\r\n",atqa);
                    if(atqa == 0x000c) 
                    {
                        NFC_A_Tag1_Test(g_tag_info.serial_num);
                        demo_step = 0;
                        card_det  = 1;
                        break;
                    }
                    else if(atqa == 0x4400) 
                    {
                        NFC_A_Tag2_Test(g_tag_info.serial_num);
                        demo_step = 0;
                        card_det  = 1;
                        break;
                    }
                }
                g_ret = pcd_active(1);
                if(g_ret == 0) {
                    demo_step = 2;
                } else {
                    demo_step = 0;
                }
                break;
            case 2:
                if(card_A) 
                {
                    Iblock_getrand(1);
                }
                if(card_B) 
                {
                    g_ask_block_flag = 1;
                    g_ret = get_idcard_num(g_tag_info.serial_num);
                    if(g_ret == 0) {
                        g_tag_info.uid_length = 10;
                        PRINT_BUF("ID_NUM: ", g_tag_info.serial_num, g_tag_info.uid_length);
                        printf("## test end##\r\n\r\n");
                    } 
                    else 
                    {
                        Iblock_getrand(1);
                    }
                }
                if(card_F) 
                {
                    NFC_F_Tag3_Test(g_tag_info.serial_num);
                }
                if(card_V) 
                {
                    NFC_V_Tag5_Test(g_tag_info.serial_num);
                }
                demo_step = 0;
                card_det  = 1;
                break;
            default:
                demo_step = 0;
                card_det  = 0;
                break;
        }
        if(demo_step == 0) {
            pcd_antenna_off();
            delay_1ms(1);
            #ifdef SPD_MODE
            pcd_sleep();      //Soft power off
            #endif
            break;
        }
    }
    return card_det;
}

void lpcd_demo(void) {
    uint8_t adc_value;
    uint32_t wait_cnt = 0;
    irq_flag_io = 0;
    lpcd_cfg.t1 = LPCD_DETECT_PERIOD;      //0x12 300ms; //0x20 500ms ; //0x38 800ms
    lpcd_cfg.sense = LPCD_DETECT_SENS;
    printf("******* start lpcd_demo() *******\r\n");
    osc_calib();
    lpcd_init(1, 0x40); //fst_en
    lpcd_entry();
    while(1) {
        #ifdef NOT_IRQ
        if(!(INT_PIN == 0))
        #else
        if(irq_flag_io)
        #endif
        {
            printf("...................................\r\n");
            printf("\r\n******* 1. pcd_exit() *********\r\n");
//            irq_flag_io = 0;
            adc_value = lpcd_exit();
            Rfid_Init();
            printf("******* 2. nfc_demo() *********\r\n");
            lpcd_init(nfc_demo(), adc_value);
            pcd_antenna_off();
            printf("******* 3. lpcd_entry() *******\r\n");
            lpcd_entry();
            // DWT_DELAY_MS(100);     //调节检测灵敏度
            delay_1ms(100);
            irq_flag_io = 0;
            
        } else {
            wait_cnt++;
            if(wait_cnt == 500000) {
                wait_cnt = 0;
                printf(".");
            }
        }
        
    }
}

/**
 * @brief  sl2823数据交互测试
 * @param  无
 * @retval 无
 */
void sl2823_test(void) 
{
    printf("\r\nSoftware Version:2024.6.\r\n");
    // DWT_DELAY_MS(2); 
    delay_1ms(2);
    spi_wr_test();
    Rfid_Init();
    pcd_default_info();
    irq_flag_io = 0;
    #ifdef LPCD_MODE
        lpcd_demo();
    #else
        while(1) 
        {
            nfc_demo();
            printf(".");
            // DWT_DELAY_MS(200);     //调节检测灵敏度
            delay_1ms(200);
        }
    #endif
}

#ifndef USE_STDPERIPH_DRIVER
// int main() {
//     sl2823_test();
// }
#endif
